package com.pengtu.service.project;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.pengtu.dao.Page;
import com.pengtu.dao.QueryFilter;
import com.pengtu.dao.app.OrganizationDao;
import com.pengtu.dao.project.ConstructOrganizationDao;
import com.pengtu.dao.project.DesignOrganizationDao;
import com.pengtu.dao.project.DetectOrganizationDao;
import com.pengtu.dao.project.OwnerOrganizationDao;
import com.pengtu.dao.project.QualificationDao;
import com.pengtu.dao.project.SupervisorOrganizationDao;
import com.pengtu.entity.app.Organization;
import com.pengtu.entity.project.ConstructOrganization;
import com.pengtu.entity.project.DesignOrganization;
import com.pengtu.entity.project.DetectOrganization;
import com.pengtu.entity.project.OwnerOrganization;
import com.pengtu.entity.project.Qualification;
import com.pengtu.entity.project.SupervisoryOrganization;

@Component
@Transactional
public class SignOrganizationManager {

	private OwnerOrganizationDao ownerOrganizationDao;
	private ConstructOrganizationDao constructOrganizationDao;
	private DesignOrganizationDao designOrganizationDao;
	private SupervisorOrganizationDao supervisorOrganizationDao;
	private QualificationDao qualificationDao;
	private OrganizationDao organizationDao;
	private DetectOrganizationDao detectOrganizationDao;
	@Autowired
	public void setDetectOrganizationDao(DetectOrganizationDao detectOrganizationDao) {
		this.detectOrganizationDao = detectOrganizationDao;
	}

	@Autowired
	public void setOrganizationDao(OrganizationDao organizationDao) {
		this.organizationDao = organizationDao;
	}

	@Autowired
	public void setQualificationDao(QualificationDao qualificationDao) {
		this.qualificationDao = qualificationDao;
	}

	@Autowired
	public void setSupervisorOrganizationDao(
			SupervisorOrganizationDao supervisorOrganizationDao) {
		this.supervisorOrganizationDao = supervisorOrganizationDao;
	}

	@Autowired
	public void setDesignOrganizationDao(
			DesignOrganizationDao designOrganizationDao) {
		this.designOrganizationDao = designOrganizationDao;
	}

	@Autowired
	public void setOwnerOrganizationDao(
			OwnerOrganizationDao ownerOrganizationDao) {
		this.ownerOrganizationDao = ownerOrganizationDao;
	}

	@Autowired
	public void setConstructOrganizationDao(
			ConstructOrganizationDao constructOrganizationDao) {
		this.constructOrganizationDao = constructOrganizationDao;
	}

	// ///*****操作OwnerOrgnzition对象********************////
	/**
	 * 根据属性获取单条数据
	 */
	public OwnerOrganization getOwner(String propertyName, String value) {
		if (propertyName.equalsIgnoreCase("id"))
			return ownerOrganizationDao.get(value);
		return ownerOrganizationDao.findUniqueBy(propertyName, value);
	}
	
	public Page<OwnerOrganization> findPageOwner(final  Page<OwnerOrganization> page,
			final QueryFilter queryfilters) {
		// TODO Auto-generated method stub
		return ownerOrganizationDao.findPage(page, queryfilters);
	}

	// /***********操作ConstructOrganization对象******************///
	public ConstructOrganization getConstruct(String propertyName, String value) {
		// TODO Auto-generated method stub
		if (propertyName.equalsIgnoreCase("id"))
			return constructOrganizationDao.get(value);
		return null;
	}

	public Page<ConstructOrganization> findPageConstruction(
			final Page<ConstructOrganization> page,
			final QueryFilter queryfilter) {
		// TODO Auto-generated method stub
		return constructOrganizationDao.findPage(page, queryfilter);
	}

	/**
	 * 删除施工单位信息
	 * 
	 * @param id
	 */
	public void deleteConstructOrg(String id) {
		// TODO Auto-generated method stub
		constructOrganizationDao.delete(id);
	}

	/**
	 * 保存OR修改施工单位信息
	 * 
	 * @param constructOrg
	 */
	public void saveConstructOrg(ConstructOrganization constructOrg) {
		// TODO Auto-generated method stub
		constructOrganizationDao.save(constructOrg);
	}

	// /***********操作DesignOrganization对象******************///
	/**
	 * 根据属性获得值对象
	 */
	@Transactional(readOnly = true)
	public DesignOrganization getDesign(String propertyName, String value) {
		// TODO Auto-generated method stub
		if (propertyName.equalsIgnoreCase("id"))
			return designOrganizationDao.get(value);
		return designOrganizationDao.findUniqueBy(propertyName, value);
	}

	/**
	 * 保存OR修改设计单位信息
	 * 
	 * @param designOrg
	 */
	public void saveDesignOrg(DesignOrganization designOrg) {
		// TODO Auto-generated method stub
		designOrganizationDao.save(designOrg);
	}

	/**
	 * 删除设计单位信息
	 * 
	 * @param id
	 */
	public void deleteDesignOrg(String id) {
		// TODO Auto-generated method stub
		designOrganizationDao.delete(id);
	}
	
	/**
	 * 分页获取DesignOrg对象
	 * @param page
	 * @param queryfilters
	 * @return
	 */
	public Page<DesignOrganization> findPageDesignOrg(
			final Page<DesignOrganization> page, final QueryFilter queryfilters) {
		// TODO Auto-generated method stub
		return designOrganizationDao.findPage(page, queryfilters);
	}

	// /***********操作SupervisorOrganization对象******************///

	/**
	 * 按属性值查找单条信息
	 */
	@Transactional(readOnly = true)
	public SupervisoryOrganization getSupervisory(String propertyName,
			String value) {
		// TODO Auto-generated method stub
		if (propertyName.equalsIgnoreCase("id"))
			return supervisorOrganizationDao.get(value);
		return supervisorOrganizationDao.findUniqueBy(propertyName, value);
	}

	/**
	 * 保存OR修改监理单位信息
	 * 
	 * @param designOrg
	 */
	public void saveSupervisorOrg(SupervisoryOrganization supervisory) {
		// TODO Auto-generated method stub
		supervisorOrganizationDao.save(supervisory);
	}

	/**
	 * 删除设计监理信息
	 * 
	 * @param id
	 */
	public void deleteSupervisorOrg(String id) {
		// TODO Auto-generated method stub
		supervisorOrganizationDao.delete(id);
	}

	@Transactional(readOnly = true)
	public Page<SupervisoryOrganization> findPageSupervisoryOrg(
			final Page<SupervisoryOrganization> page,
			final QueryFilter queryfilters) {
		// TODO Auto-generated method stub
		return supervisorOrganizationDao.findPage(page, queryfilters);
	}

	// -----------------操作建设单位信息
	/**
	 * 增加或修改建设单位信息
	 */
	public void saveOwner(OwnerOrganization ownerOrganization) {
		// TODO Auto-generated method stub
		ownerOrganizationDao.save(ownerOrganization);
	}

	/**
	 * 删除建设单位信息
	 */
	public void deleteOwner(String id) {
		// TODO Auto-generated method stub
		ownerOrganizationDao.delete(id);
	}

	@Transactional(readOnly = true)
	public List<OwnerOrganization> findAllOwner() {
		// TODO Auto-generated method stub
		return ownerOrganizationDao.find();
	}

	@Transactional(readOnly = true)
	public List<OwnerOrganization> findByOwner(String propertyName, String value) {
		// TODO Auto-generated method stub
		return ownerOrganizationDao.findBy(propertyName, value);
	}

	// *************(((((操作qualification对象)))))))))))))))))))
	@Transactional(readOnly = true)
	/**
	 * 根据属性获取单条信息
	 */
	public Qualification getQualification(String propertyName, String value) {
		// TODO Auto-generated method stub
		if (propertyName.equalsIgnoreCase("id"))
			return qualificationDao.get(value);
		return qualificationDao.findUniqueBy(propertyName, value);
	}

	/**
	 * 分页获取
	 * 
	 * @param page
	 * @param queryfilters
	 * @return
	 */
	@Transactional(readOnly = true)
	public Page<Qualification> findPageQualification(
			final Page<Qualification> page, final QueryFilter queryfilters) {
		// TODO Auto-generated method stub
		return qualificationDao.findPage(page, queryfilters);
	}

	/**
	 * 保存或修改数据信息
	 * 
	 * @param qualification
	 */
	public void saveQualification(Qualification qualification) {
		qualificationDao.save(qualification);
	}

	/**
	 * 删除对象
	 * 
	 * @param id
	 */
	public void deleteQualification(String id) {
		qualificationDao.delete(id);
	}

	/**
	 * 判断证书编号是否重复
	 * 
	 * @param code
	 * @param oldCode
	 */
	public boolean isQuaCodeUnique(String code, String oldCode) {
		// TODO Auto-generated method stub
		return qualificationDao.isPropertyUnique("code", code, oldCode);
	}

	/**
	 * 按属性获取单条单位信息
	 * 
	 * @param string
	 * @param organizatinId
	 * @return
	 */
	@Transactional(readOnly = true)
	public Organization getOrg(String propertyName, String value) {
		// TODO Auto-generated method stub
		if (propertyName.equalsIgnoreCase("id"))
			return organizationDao.get(value);
		return organizationDao.findUniqueBy(propertyName, value);
	}

	// 判断机构代码是否重复
	public boolean isOrgNumUnique(String orgNum, String oldOrgNum) {
		// TODO Auto-generated method stub
		return organizationDao.isPropertyUnique("orgNum", orgNum, oldOrgNum);
	}
	/**
	 * 判断单位名称是否重复
	 * @param orgName
	 * @param oldOrgName
	 * @return
	 */
	public boolean isOrgNameUnique(String orgName, String oldOrgName) {
		// TODO Auto-generated method stub
		return organizationDao.isPropertyUnique("orgName", orgName, oldOrgName);
	}

	
	//********通用方法*************//
	public void saveAllOrganization(Organization org){
		if(null!=org){
			if(org  instanceof OwnerOrganization){
				ownerOrganizationDao.save((OwnerOrganization) org);
			}else if (org instanceof ConstructOrganization){
				constructOrganizationDao.save((ConstructOrganization) org);
			}else if (org instanceof SupervisoryOrganization){
				supervisorOrganizationDao.save((SupervisoryOrganization) org);
			}else if (org instanceof DesignOrganization ){
				 designOrganizationDao.save((DesignOrganization) org);
			}else{
				organizationDao.save(org);
			}	
		}
	}

	public Page<DetectOrganization> findPageDetection(Page<DetectOrganization> page, QueryFilter fileter) {
		return detectOrganizationDao.findPage(page, fileter);
	}

	public DetectOrganization getDetect(String propertyName, String value) {
		if (propertyName.equalsIgnoreCase("id"))
			return detectOrganizationDao.get(value);
		return detectOrganizationDao.findUniqueBy(propertyName, value);
	}

	public void deleteDetectOrg(String id) {
		detectOrganizationDao.delete(id);
		
	}

	public void saveDetectOrg(DetectOrganization entity) {
		detectOrganizationDao.save(entity);
		
	}

	
}
